package com.atguigu.gmall.realtime.util;

import com.atguigu.gmall.realtime.common.Constant;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.flink.shaded.guava18.com.google.common.base.CaseFormat;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author lzc
 * @Date 2023/2/10 10:30
 */
public class JdbcUtil {
    
    public static Connection getPhoenixConnection() {
        String driver = Constant.PHOENIX_DRIVER;
        String url = Constant.PHOENIX_URL;
        
        
        return getJdbcConnection(driver, url, null, null);
    }
    
    private static Connection getJdbcConnection(String driver,
                                                String url,
                                                String user,
                                                String password) {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("你提供的 jdbc 驱动不存在: " + driver);
        }
    }
    
    public static void closeConnection(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static Connection getMySqlConnection() {
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        return getJdbcConnection(driver, url, "root", "aaaaaa");
    }
    
    public static <T> List<T> queryList(Connection conn,
                                        String querySql,
                                        Object[] args,
                                        Class<T> tClass,
                                        boolean... isUnderLineToCamel) throws Exception {
        boolean flag = false;
        if (isUnderLineToCamel.length == 1) {
            flag = isUnderLineToCamel[0];
        }
        
        ArrayList<T> list = new ArrayList<>();
        PreparedStatement ps = conn.prepareStatement(querySql);
        // 给占位符赋值
        for (int i = 0; args != null && i < args.length; i++) {
            Object arg = args[i];
            ps.setObject(i + 1, arg);
        }
        // id  name  age
        // 1   zs   10
        // 2   lisi  10
        ResultSet resultSet = ps.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData(); // 表结构: 列名, 列的类型, 多少列 ...
        
        while (resultSet.next()) {  // 遍历所有行
            T t = tClass.newInstance();  // 利用无参构造器创建对象
            list.add(t);
            // 遍历所有的列
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                // 获取列名: 列名要和T类型的属性名保持一致
                String name = metaData.getColumnName(i);
                if (flag) {
                    // 下划线转成驼峰  a_a =>aA aa_b =>aaB
                    name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
                }
                
                Object v = resultSet.getObject(i);
                BeanUtils.setProperty(t, name, v); // 给对象的属性赋值: t.name=v
            }
        }
    
        ps.close();
        
        return list;
    }
    
    public static void main(String[] args) throws Exception {
        List<HashMap> list = queryList(getPhoenixConnection(),
                                   "select * from dim_sku_info where id=?",
                                   new Object[]{"1"},
                                   HashMap.class,
                                   false
        );
        
        for (Object tp : list) {
            
            System.out.println(tp);
        }
        
    }
}
